package com.hzcx.app.simplechat.ui.friend;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.base.BaseActivity;
import com.hzcx.app.simplechat.ui.friend.adapter.FriendSortAdapter;
import com.hzcx.app.simplechat.ui.friend.bean.FriendCityBean;
import com.hzcx.app.simplechat.ui.friend.contract.FriendSortContract;
import com.hzcx.app.simplechat.ui.friend.dialog.FriendSortDialog;
import com.hzcx.app.simplechat.ui.friend.presenter.FriendSortPresenter;
import com.hzcx.app.simplechat.ui.user.UserInfoActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 朋友列表排序界面
 */
public class FriendSortActivity extends BaseActivity<FriendSortPresenter> implements FriendSortContract.View {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.tv_sort)
    TextView tvSort;
    @BindView(R.id.rv_sort)
    RecyclerView rvSort;
    @BindView(R.id.cons_main)
    ConstraintLayout consMain;

    private List<FriendCityBean> friendData;
    private FriendSortAdapter sortAdapter;

    @Override
    public int getLayout() {
        return R.layout.activity_friend_sort;
    }

    @Override
    public void initPresenter() {
        mPresenter = new FriendSortPresenter();
    }

    @Override
    public void initView() {
        tvTitle.setText("联系人");

        friendData = new ArrayList<>();
        sortAdapter = new FriendSortAdapter(friendData);
        rvSort.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        rvSort.setAdapter(sortAdapter);
    }

    @Override
    public void initData() {
        mPresenter.getFriendList(this, 1);

        sortAdapter.setOnItemClickListener((adapter, view, position) -> startActivity(new Intent(FriendSortActivity.this, UserInfoActivity.class)
                .putExtra(UserInfoActivity.INTENT_USER_ID, friendData.get(position).getMember_id())));
    }

    @OnClick({R.id.tv_sort})
    public void viewOnClick(View view) {
        switch (view.getId()) {
            case R.id.tv_sort:
                FriendSortDialog sortDialog = new FriendSortDialog(this, type -> mPresenter.getFriendList(FriendSortActivity.this, type));
                sortDialog.show();
                break;
        }
    }

    /**
     * 好友列表获取成功
     *
     * @param data
     * @param type
     */
    @Override
    public void friendListResult(List<FriendCityBean> data, int type) {
        try {
            friendData.clear();
            friendData.addAll(data);
            sortAdapter.notifyDataSetChanged();
            if (data.size() > 0) {
                tvTitle.setText("联系人(" + data.size() + ")");
            } else {
                tvTitle.setText("联系人");
            }
            consMain.setVisibility(View.VISIBLE);
            if (type == 1) {
                tvSort.setText("在线时间排序");
            } else {
                tvSort.setText("添加时间排序");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
